<?php

namespace data\Service\cart;

use data\Model\cart\ShopCartModel;
use data\Service\BaseService;
use data\Service\common\CommonService as CommonService;
use data\Service\goods\Product as GoodsService;
use data\Service\store\StoreService as StoreService;

class ShopCart extends BaseService {
	
	private $cart;
	private $parents;
	private $common;
	private $goods;
	private $store;
	
    function __construct()
    {
        parent::__construct();
        $this->cart = new ShopCartModel();
    }
	
    /*
     * 
	 *查询一条购物车
     */	
    public function getCartInfo($str, $arr, $field)
    {
		$result = $this->cart->getInfo($str, $arr, $field);
        return $result;
    }
	
    /*
     * 
	 *查询所有购物车
     */
    public function getCartList($str, $arr, $field, $order)
    {
		$list = $this->cart->getQuery($str, $arr, $field, $order);
        return $list;
    }
	
    /*
     * 查询购物车总数
     * 
     */
    public function getCartCount($str='', $arr=array())
    {
        $count = $this->cart->where($str, $arr)->count();
        return $count;
    }
	
    /*
     * 字段统计
     * 
     */
    public function getCartSum($str='', $arr=array(), $name)
    {
        $count = $this->cart->where($str, $arr)->sum($name);
        return $count;
    }
	
    /*
     * 添加数据
     * 
     */
	public function getCartAdd($data){
		$save = $this->cart->getAdd($data);
		return $save ;
	}
	
    /*
     * 更新
     * 
     */
	public function getCartSave($str, $arr, $data){
		$save = $this->cart->getSave($str, $arr, $data);
		return $save ;
	}
	
    /*
     * 删除
     * 
     */
	public function getCartDel($str, $arr){
		$del = $this->cart->getDel($str, $arr);
		return $del ;
	}
	
    /*
     * 累加
     * 
     */
	public function getCartInc($str, $arr, $name, $data){
		$Inc = $this->cart->getSetInc($str, $arr, $name, $data);
		return $Inc ;
	}
	
    /*
     * 累减
     * 
     */
	public function getCartDec($str, $arr, $name, $data){
		$Dec = $this->cart->getSetDec($str, $arr, $name, $data);
		return $Dec ;
	}

    /*
     * 数据联查
     * 
     */
	public function getCartJson($table, $join, $str, $arr, $field, $order){
		
		$Json = $this->cart->getJson($table, $join, $str, $arr, $field, $order);
		return $Json ;
	}	
    /*
     * 浏览器购物车产品 
     * 加入
	 * 购物车数据表
	 *
     */
    public function historyCartAdd($goodsnum)
    {
		$this->parents = new BaseService();
		
        $history = $this->parents->strAnalysis(cookie('_cart')) ;
		
		if ($history[0]['goods_id']) {
			
			foreach ($history as $key=>$vv) {
				//不能购买自己店铺的产品
				if ($this->store_id === $vv ['store_id']) {
					unset($vv) ;
				}
				
				//购物车已存在该商品，则累加数量
				$str = 'cart_id>%d and buyer_id=%d and goods_id=%d' ;
				$arr = array(0, $this->uid, $vv['goods_id']);
				$cart_l = $this->getCartInfo($str, $arr, '*') ;
				unset($str);
				unset($arr);
				if ($cart_l && $cart_l['goods_price'] === $vv['goods_price'] && $cart_l['colorkey'] === $vv['colorkey'] && $cart_l['speckey'] === $vv['speckey'] && $cart_l['color_n'] === $vv['color_n'] && $cart_l['spec_n'] === $vv['spec_n']) {
					
					//数量累加
					$goodsAmount = $cart_l['goods_num'] + $vv['goods_num'] ;
					//库存不足
					if ($goodsAmount > $goodsnum) {
						unset($vv) ;
					} else {
						$this->getCartInc('cart_id=%d', array($cart_l['cart_id']), 'goods_num', $vv['goods_num']);
					}
					unset($vv) ;
					unset($goodsAmount) ;
				}
				
				unset($cart_l) ;
				
				//将浏览器的产品加入购物车
				if ($vv['goods_id'] && $vv ['goods_num']>0) {
					
					$data ['buyer_id'] = $this->uid ;
					$data ['store_id'] = $vv ['store_id'] ;
					//店铺名
					$data ['store_name'] = $vv['store_name'] ;			
					$data ['goods_id'] = $vv ['goods_id'] ;
					$data ['goods_name'] = $vv ['goods_name'] ;
					$vv ['goods_price'] && $data ['goods_price'] = $vv ['goods_price'] ;
					$vv ['point_num'] && $data ['point_num'] = $vv ['point_num'] ;
					$vv ['coin_num'] && $data ['coin_num'] = $vv ['coin_num'] ;
					$data ['goods_num'] = $vv ['goods_num'] ;
					$data ['goods_image'] = $vv ['goods_image'] ;
					$vv ['colorkey'] && $data ['colorkey'] = $vv ['colorkey'] ;
					$vv ['speckey'] && $data ['speckey'] = $vv ['speckey'] ;
					$vv ['color_n'] && $data ['color_n'] = $vv ['color_n'] ;
					$vv ['spec_n'] && $data ['spec_n'] = $vv ['spec_n'] ;
					
					$this->getCartAdd($data);
					unset($data) ;
				}							
			}			
			
			//清楚浏览器cookie
			setcookie('_cart');
			setcookie('_cart_goods_num');
			setcookie('_cart_goods_amount');
			setcookie('_cart_goods_amount_coins');			
			return false;
		}
    }
	
	 /*
     * 加入购物车
	 * 直接加入购物车数据表
	 *
     */
	 public function getAddCar($goodsInfo, $specArr) {
		 
		if ($goodsInfo) {			
			
			$edit = $this->getEditCar($goodsInfo, 0, $specArr);
		
			if (!$edit && $goodsInfo ['goods_num']>0) {
				
				$data ['buyer_id'] = $this->uid ;
				$data ['store_id'] = $goodsInfo ['st_id'] ;
				//店铺名			
				$data ['store_name'] = $goodsInfo['store_name'] ;			
				$data ['goods_id'] = $goodsInfo ['id'] ;
				$data ['goods_name'] = $goodsInfo ['productname'] ;
				$goodsInfo ['price'] && $data ['goods_price'] = $goodsInfo ['price'] ;
				$data ['goods_num'] = $goodsInfo ['goods_num'] ;
				$data ['goods_image'] = $goodsInfo ['pic'] ;
				$goodsInfo ['point_num'] && $data ['point_num'] = $goodsInfo ['point_num'] ;
				$goodsInfo ['coin_num'] && $data ['coin_num'] = $goodsInfo ['coin_num'] ;
				$data ['minus'] = $goodsInfo ['minus'] ;
				$specArr['colorid'] &&  $data ['colorkey'] = $specArr['colorid'] ;
				$specArr['specid'] &&  $data ['speckey'] = $specArr['specid'] ;
				$specArr['color_n'] &&  $data ['color_n'] = $specArr['color_n'] ;
				$specArr['spec_n'] &&  $data ['spec_n'] = $specArr['spec_n'] ;
				
				$add = $this->getCartAdd($data);				
			}
			unset($goodsInfo) ;			
			return true ;			
		}		 
	 }
	 
	 /*
     * 加入购物车
	 * 直接加入购物车数据表
	 *
     */
	 public function getEditCar($goodsInfo, $goods_num, $specArr) {
		 
	    $edit = false ;
		$c_list = array();
		
		if ($goodsInfo) {

			//购物车无产品
			if ($goodsInfo['id'] && !$goodsInfo['cart_id']) {
				
				$str = "cart_id>%d and buyer_id=%d and goods_id=%d and colorkey='%s' and speckey='%s' and color_n='%s' and spec_n='%s'";
				$arr = array(0,$this->uid,$goodsInfo['id'],$specArr['colorid'],$specArr['specid'],$specArr['color_n'],$specArr['spec_n']);				
				$c_list = $this->getCartInfo($str, $arr, '*') ;
				
			}else if ($goodsInfo['cart_id']) {
			//购物车有产品					
				$c_list['cart_id'] = $goodsInfo['cart_id'] ;
				$c_list['goods_num'] = $goods_num ;
			} 

			if ($c_list) {
				
				//数量是否改变
				if ($goodsInfo ['change']) {
					//数量
					$data['goods_num'] = $goodsInfo['goods_num'] ;
				} else {
					//数量
					
					$data['goods_num'] = $c_list['goods_num'] + $goodsInfo['goods_num'] ;
				}
										
				$c_list['cart_id'] && $edit = $this->getCartSave('cart_id=%d', array($c_list['cart_id']), $data);
				//unset($data) ;
			}			
			return $edit ;			
		}		 
	 }
	 
	 /*
     * 加入购物车
	 * 直接加入购物车数据表
	 *
     */
	 public function shopCartDtata($allGoods, $goodsInfo, $goods_num, $specArr) {
		 
			$i = 0 ;
			$amount = 0 ;
			
			$colorid = $specArr['colorid'] ;
			$specid = $specArr['specid'] ;
			$color_n = $specArr['color_n'] ;
			$spec_n = $specArr['spec_n'] ;
			
			if(!$allGoods){
				//将数据保存到 $allGoods
				$allGoods[0]['goods_price'] = $goodsInfo ['price'] ;//单价
				$allGoods[0]['goods_name'] = $goodsInfo ['productname'] ;
				$allGoods[0]['goods_image'] = $goodsInfo ['pic'] ;
				$allGoods[0]['store_id'] = $goodsInfo ['st_id'] ;
				$allGoods[0]['goods_id'] = $goodsInfo ['id'] ;
				$allGoods[0]['goods_num'] = $goods_num ;
				$price = $goods_num * $goodsInfo['price'] ;
				$allGoods[0]['amount'] = $price ;//总价
				$allGoods[0]['minus'] = 0 ;
				$colorid ? $allGoods[0]['colorkey'] = $colorid :$allGoods[0]['colorkey'] = 0;//颜色关键字
				$specid ? $allGoods[0]['speckey'] = $specid : $allGoods[0]['speckey'] = 0;//规格关键字
				$allGoods[0]['store_name'] = $goodsInfo ['store_name'] ;//单价
				
				$amount = $allGoods[0]['amount'] ;				
			}else{
				
				//将数据保存到 $allGoods数组中
				
				//数据存在$allGoods数组中
				foreach ($allGoods as $key=>$v) {

					if ($v['goods_id']===$goodsInfo['id'] && $v['goods_price']===$goodsInfo['price'] && $v['colorkey']===$goodsInfo['colorkey'] && $v['speckey']===$goodsInfo['speckey'] && $v['color_n']===$goodsInfo['color_n'] && $v['spec_n']===$goodsInfo['spec_n']) {
						
						unset($i);
						$i = $i+1 ;	
						if ($goodsInfo['change']) {
							//数量
							$allGoods [$key]['goods_num'] = $goods_num ;
						} else {
							//数量
							$allGoods [$key]['goods_num'] = $v['goods_num'] + $goods_num ;
						}
												
					    //总价
						$allGoods [$key]['point_num'] = $goodsInfo['point_num'] ;
						$allGoods [$key]['coin_num'] = $goodsInfo['coin_num'] ;			
						$allGoods [$key]['amount_points'] = $goodsInfo['point_num'] *  $allGoods [$key]['goods_num'];
						$allGoods [$key]['amount_coin'] = $goodsInfo['coin_num'] * $allGoods [$key]['goods_num'];
						$allGoods [$key]['amount'] = $allGoods [$key]['goods_num'] * $v['goods_price'];//商品总价
					 }
					 //所有商品总价
					 $amount += $allGoods [$key]['amount'] ;
					 $amountPoints += $allGoods [$key]['amount_points'] ;
					 $amountCoin += $allGoods [$key]['amount_coin'] ;
				}
				
				//数据不存在$allGoods数组中
				if (!$i) {
					$newGoods['goods_price'] = $goodsInfo ['price'] ;
					$newGoods['goods_name'] = $goodsInfo ['productname'] ;
					$newGoods['goods_image'] = $goodsInfo ['pic'] ;
					$newGoods['store_id'] = $goodsInfo ['st_id'] ;
					$newGoods['goods_id'] = $goodsInfo ['id'] ;
					$newGoods['goods_num'] = $goods_num ;//数量					
					$newGoods['minus'] = $goodsInfo['minus'] ;	
					$newGoods ['point_num'] = $goodsInfo['point_num'] ;
					$newGoods ['coin_num'] = $goodsInfo['coin_num'] ;
					$newGoods ['amount_points'] = $goodsInfo['point_num'] * $goods_num;
					$newGoods ['amount_coin'] = $goodsInfo['coin_num'] * $goods_num;	
					$price = $goods_num * $g_list['price'] ;
					$newGoods['amount'] = $price ;//总价
					$newGoods['store_name'] = $goodsInfo['store_name'] ;//总价
					
					$colorid ? $newGoods['colorkey'] = $colorid :$newGoods['colorkey'] = 0 ;
					$specid ? $newGoods['speckey'] = $specid :$newGoods['speckey'] = 0 ;
					$color_n ? $newGoods['color_n'] = $color_n :$newGoods['color_n'] = '0' ;
					$spec_n ? $newGoods['spec_n'] = $spec_n :$newGoods['spec_n'] = '0' ;
					
					//重新计算总价
					$amount = $amount + $newGoods['amount'] ;
					$amountPoints = $amountPoints + $newGoods ['amount_points'] ;
					$amountCoin = $amountCoin + $newGoods ['amount_coin'] ;
					//旧数据和新数据合并
					array_push($allGoods, $newGoods);					
				}				
			} 
			
			//将数据保存到cookie或者session中
			$type = 0 ;
			//旧购物车产品数量
			$oldNum = 0 ;
			$this->parents = new BaseService();
		
			$cookie = $this->parents->strColation($allGoods) ;			
			if ($this->uid) {
				$cookie && session('_cart', $cookie) ;
				$type = 1 ;
				$goodsInfo['change'] && $oldNum = session('_cart_goods_num') ;
			} else {
				$cookie && cookie('_cart', $cookie) ;
				$goodsInfo['change'] && $oldNum = cookie('_cart_goods_num') ;
			}
			//最新购物车产品数量
			$newNum = $goods_num + $oldNum ;
			
			if ($type) {
			   session('_cart_goods_num',$newNum) ;
			   session('_cart_goods_amount',$amount) ;
			   session('_cart_goods_amount_points',$amountPoints) ;
			   session('_cart_goods_amount_coins',$amountCoin) ;				 
			} else {				 
			   cookie('_cart_goods_num',$newNum) ;
			   cookie('_cart_goods_amount',$amount) ;
			   cookie('_cart_goods_amount_points',$amountPoints) ;
			   cookie('_cart_goods_amount_coins',$amountCoin) ;						 
			}
			
			return array('num'=>$newNum, 'amount'=>$amount, 'state'=>1, 'msg'=>'商品不存在') ;
		 
	 }
	 
	/*
	*
	*购物车内容ajax
	*
	*/
	public function ajaxCart() {
		
		$this->parents = new BaseService();
		if ($this->uid) {
			
 			//会员登录状态				
			$cart['cart_list'] = $this->parents->strAnalysis(session('_cart'));
			$cart['cart_goods_num'] = session('_cart_goods_num');
			$cart['cart_goods_amount'] = session('_cart_goods_amount');	
			$cart['_cart_goods_amount_points'] = session('_cart_goods_amount_points');
			$cart['_cart_goods_amount_coins'] = session('_cart_goods_amount_coins');
		} else {
			//浏览器保存购物车信息
			$cart['cart_list'] = $this->parents->strAnalysis(cookie('_cart'));
			$cart['cart_goods_num'] = cookie('_cart_goods_num');
			$cart['cart_goods_amount'] = cookie('_cart_goods_amount');
			$cart['_cart_goods_amount_points'] = cookie('_cart_goods_amount_points');
			$cart['_cart_goods_amount_coins'] = cookie('_cart_goods_amount_coins');			
		}
		!$cart['cart_goods_num'] && $cart['cart_goods_num'] = 0 ;
		!$cart['cart_goods_amount'] && $cart['cart_goods_amount'] = 0 ;	
		return $cart ;
	}

	
	/**
	*重新更新购物车
	*
	*/
	public function updateCarData() {
		
		$str = "cart_id>%d and buyer_id=%d and goods_id>%d" ;
		$arr = array(0,$this->uid,0);
		$cartInfo = $this->getCartList($str, $arr, '*');

		if ($cartInfo) {
			
			$this->parents = new BaseService();
			
			$cookie = $this->parents->strColation($cartInfo) ;
			$cart['cart_list'] = $this->parents->strAnalysis($cookie) ;
			session('_cart',$cookie) ;
							
			//数量
			$cartGoodsNum = $this->getCartSum($str, $arr, 'goods_num');
							
			$cart['cart_goods_num'] = $cartGoodsNum ;
			session('_cart_goods_num', $cartGoodsNum) ;
			$amount = 0 ;
			$amountPoints = 0 ;
			$amountCoin = 0 ;
			foreach ($cartInfo as $key =>$v) {
				$amount += ($v['goods_num'] * $v['goods_price']) ;
				$amountPoints += ($v['goods_num'] * $v['point_num']) ;
				$amountCoin += ($v['goods_num'] * $v['coin_num']) ;
			}
			//总金额
			$cart['cart_goods_amount'] = $amount ;
			session('_cart_goods_amount', $amount);
			session('_cart_goods_amount_points',$amountPoints);
			session('_cart_goods_amount_coins',$amountCoin);
						
		}						
		if ($cartGoodsNum) {
			echo "<script language=JavaScript> window.location.reload();</script>";
		} else {
			return $cart ;
		}
			
	}
		
	/**
	*getCartJson($table, $join, $str, $arr, $field, $order)
	*购物车列表
	*
	*/
	public function CartListJson($cList=false, $cArr=false) {
		
		if ($this->uid) {
						
			if ($cList && $cArr) {
				$str = $cList ;
				$arr = explode(',',$cArr);				
			} else {
				$str = 'buyer_id=%d' ;
				$arr = array($_SESSION ['user_id']) ;				
			}

			$table = '__SHOP_CART__ AS c' ;
			$order = 'c.store_id desc' ;
			$join['join1'] = 'LEFT JOIN __PRODUCT__ AS g ON g.id=c.goods_id' ;
			$join['join2'] = 'LEFT JOIN __STORE__ AS st ON st.store_id=c.store_id' ;
			
			$field = 'c.*,g.goods_state,g.goods_verify,g.goods_marketprice,g.goods_freight,g.goods_storage,g.number,st.store_name,st.store_state,st.store_id,g.shop_area_ids,g.give_coin,g.goods_color,g.goods_spec,g.price,g.goods_spec_arr' ;
			
			$cartList = $this->getCartJson($table, $join, $str, $arr, $field, $order) ;

			return $cartList ;			
		}
	}
	

	/**
	*getCartJson($table, $join, $str, $arr, $field, $order)
	*购物车产品属性
	*/
	public function CartInfoChange($goodsInfo) {
		
		$this->common = new CommonService();
		$cartList = array();
		$specArr = array();
		
		$cartList = $goodsInfo ;
		
		$goods_color = unserialize($goodsInfo['goods_color']);
		$goods_spec = unserialize($goodsInfo['goods_spec']);
		$specArr = unserialize($goodsInfo['goods_spec_arr']);
		$key = $goodsInfo['colorkey'].'|'.$goodsInfo['speckey'];
		//分区
		$goodsInfo ['shop_area_ids'] = unserialize($goodsInfo ['shop_area_ids']) ; 
	
		$paycode = $this->common->shopAreaStr($goodsInfo ['shop_area_ids'][0]);
				
		$gInfo = $this->common->GoodsPayPrice($paycode['payment_code'],  $goodsInfo, $specArr[$key]); 	
		
		//颜色和规格2
		$goodsInfo['colorkey'] && $cartList ['color'] = $goods_color [$goodsInfo['colorkey']]['name'] ;
		$goodsInfo['speckey'] && $cartList ['spec'] = $goods_spec [$goodsInfo['speckey']]['name'] ;
		//颜色和规格1
		$goodsInfo['color_n'] && $cartList ['color'] = $goodsInfo['color_n'] ;
		$goodsInfo['spec_n'] && $cartList ['spec'] = $goodsInfo['spec_n'] ;
					
		//商品所在店铺							
		$cartList ['amount'] = $goodsInfo['goods_num'] * $gInfo['price'];			
		$cartList ['amount_points'] = $goodsInfo['goods_num'] * $gInfo['point_num'];	
		$cartList ['amount_coin'] = $goodsInfo['goods_num'] * $gInfo['coin_num'];
		$cartList ['price'] = $gInfo['price'];
		$cartList ['point_num'] = $gInfo['point_num'];
		$cartList ['coin_num'] = $gInfo['coin_num'];
		
		//支付方式
		unset($result);
		$result = $this->common->shopPayType($goodsInfo['shop_area_ids'], $cartList);
			
		$cartList ['pay_name'] = $result['pay_name'] ;
		$cartList ['m_html'] = $result['m_html'] ;
		$cartList ['pay_id'] = $result["pay_id"] ;
		//dump($cartList);die;
		return $cartList ;
	}	

	/**
	*getCartJson($table, $join, $str, $arr, $field, $order)
	*购物车列表
	*
	*/
	public function getUserCartList() {
		
		$data = array();

		$st_id = '' ;
		$i = 0 ;		
		$cartList = $this->CartListJson();
		
		foreach ($cartList as $key=>$v) {
			
			$cartList[$key] = $this->CartInfoChange($v);
			
			$total += $cartList [$key]['amount'] ;
			$amountPoints += $cartList [$key]['amount_points'] ;

			if (!$st_id) {
				$st_id = $v['store_id'] ;
				$i = $i + 1 ;
			}
			if ($st_id==$v['store_id']) {						
				$stName = $v['store_name'];
				$stState = $v['store_state'];							
			} else {
				unset($stName);
				unset($stState);
				unset($st_id);
				$st_id = $v['store_id'] ;
				$i = $i + 1 ;
				$stName = $v['store_name'];
				$stState = $v['store_state'];
			}
			
			$cartSt[$i-1]['store_name'] = $stName ;
			$cartSt[$i-1]['store_state'] = $stState ;
			$cartSt[$i-1]['store_id'] = $st_id ;
			$cartSt[$i-1]['son_c'][] = $cartList [$key] ;	
		}

		$data ['amount_points'] = $amountPoints ;
		$data ['total'] = $total ;
		$data ['cart_list'] = $cartSt ;
		return $data ;
		
	}
	
	/**
	 * 购物车产品信息
	 *
	 * @param array $buy_items 商品ID => 购买数量
	 */
	public function goodsCartInfo($cList, $cArr) {
		
		$cartList = array();
		$cartList = $this->CartListJson($cList, $cArr);
		
		if (!$cartList) {					
			return false;
		}
		$st_id = '' ;
		$i = 0 ;	
		
		foreach ($cartList as $key=>$v) {
			
			$cartList[$key] = $this->CartInfoChange($v);
					
			//商品所在店铺			
			$amount_points[$v["store_id"]] += $cartList [$key]['amount_points'] ;//商品积分
			$amount_coin[$v["store_id"]] += $cartList [$key]['amount_coin'] ;//增值币			
			$total[$v["store_id"]] += $cartList [$key]['amount'] ;//商品金额
			
			$v['goods_freight'] = floatval($v['goods_freight']);
			if (!$goods_freight[$v["store_id"]]) {
				$goods_freight[$v["store_id"]] = $v['goods_freight'] ;//物流运费
			}
			if ($v['goods_freight']>$goods_freight[$v["store_id"]]) {
				$goods_freight[$v["store_id"]] = $v['goods_freight'] ;//物流运费
			}									
			if (!$st_id) {
				$st_id = $v['store_id'] ;
				$i = $i + 1 ;
			}
				
			if ($st_id==$v['store_id']) {						
				$st_name = $v['store_name'];
				$st_state = $v['store_state'];							
			} else {
				unset($st_name);
				unset($st_state);
				unset($st_id);
				$st_id = $v['store_id'] ;
				$i = $i + 1 ;
				$st_name = $v['store_name'];
				$st_state = $v['store_state'];
			}
			
			//店铺信息
			$cart_st[$i-1]['store_name'] = $st_name ;
			$cart_st[$i-1]['store_state'] = $st_state ;
			$cart_st[$i-1]['store_id'] = $st_id ;
			//商品信息	
			$cart_st[$i-1]['son_c'][] = $cartList [$key] ;
			//总运费	
			$goods_freight2 += $goods_freight[$v["store_id"]] ;				
			$total2 = $total[$v["store_id"]] ;
				
			$pay_id = $cartList [$key]['pay_id'] ;

			if ($storeid!==$v["store_id"]) {
					
				$storeid = $v["store_id"] ;
				//商品积分
				 $allpoints += $allpoints ;
				 //增值币
				 $allcoins += $allcoins ;
			 } else {
				//商品积分					 
				$allpoints = $amount_points[$v["store_id"]] ;
				 //增值币
				$allcoins = $amount_coin[$v["store_id"]] ;
			} 
		}
		
		$alltotal = $goods_freight2 + $total2 ;//本店合计
			
		$result ['goods_freight'] = $goods_freight ;
		$result ['total'] = $total ;
		$result ['alltotal'] = $alltotal ;
		$result ['allpoints'] = $allpoints ;
		$result ['amount_points'] = $amount_points ;
		$result ['amount_coin'] = $amount_coin ;
		$result ['allcoins'] = $allcoins ;
		$result ['cart_st'] = $cart_st ;
		//最后一个为支付方式
		$result ['pay_id'] = $pay_id ;
				
			return $result ;		
		
	}
	
	/*
	* 立即购买单个产品信息
	*
	*/
	public function atBuyGoodsInfo($gid, $goods_nums, $specArr=false) {
		
		$str = 0 ;
		$arr = array() ;
		$spec_arr = array();
		
		$this->goods = new GoodsService();
		$this->store = new StoreService();
		if ($specArr) {
			
			$str = 'id=%d and goods_state=%d and goods_verify=%d' ;
			$arr = array($gid,1,1) ;
			$goods_bill = $this->goods->getGoodsInfo($str, $arr, '*');
			
			if (!$specArr['colorid'] && !$specArr['specid']) {

				if ($goods_bill['goods_storage']<$goods_nums) {
					return 4;//库存不足
				}				
			} else {
				$spec_arr = unserialize($goods_bill['goods_spec_arr']);
				$key = '' ;
				$key = $specArr['colorid'].'|'.$specArr['specid'] ;				
				if ($spec_arr[$key]['stock']<$goods_nums) {
					return 4;//库存不足
				}
			}

			unset($str);
			unset($arr);
			$str = 'store_id=%d and store_state=%d' ;
			$arr = array($goods_bill['st_id'], 1);
			$goods_bill && $store = $this->store->getStoreInfo($str, $arr, '*'); 
				
			$specArr['colorid'] ? $goods_bill['colorkey'] = $specArr['colorid'] : $goods_bill['colorkey'] = 0;
			$specArr['specid'] ? $goods_bill['speckey'] = $specArr['specid'] : $goods_bill['speckey'] = 0;
			$specArr['color_n'] ? $goods_bill['color_n'] = $specArr['color_n'] : $goods_bill['color_n'] = '0';
			$specArr['spec_n'] ? $goods_bill['spec_n'] = $specArr['spec_n'] : $goods_bill['spec_n'] = '0';
			
			$goods_bill = $this->CartInfoChange($goods_bill);			
		} else {
			$goods_bill = $_SESSION['cart_list'.$_SESSION['user_id']];
		}
			
		$result['goods_num'] = $goods_nums ;
		$result['goods_id'] = $gid ;
		$result ['cart_st'][0]['store_name'] = $store ['store_name'] ;
		$result ['cart_st'][0]['store_id'] = $store ['store_id'] ;				
		$result ['cart_st'][0]['son_c'][0]['store_name'] = $store ['store_name'] ;
		$result ['cart_st'][0]['son_c'][0]['store_id'] = $store ['store_id'] ;
		$result['cart_st'][0]['son_c'][0]['goods_num'] = $goods_nums ;
		$result['cart_st'][0]['son_c'][0]['goods_id'] = $goods_bill['id'] ;
		$result['cart_st'][0]['son_c'][0]['goods_image'] = $goods_bill['pic'] ;
		$result['cart_st'][0]['son_c'][0]['goods_name'] = $goods_bill['productname'] ;
		$result['cart_st'][0]['son_c'][0]['goods_price'] = $goods_bill['price'] ;			
		$result['cart_st'][0]['son_c'][0]['point_num'] = $goods_bill['point_num'] ;
		$result['cart_st'][0]['son_c'][0]['coin_num'] = $goods_bill['coin_num'] ;
		$result['cart_st'][0]['son_c'][0]['give_coin'] = $goods_bill['give_coin'] ;			
		$result['cart_st'][0]['son_c'][0]['pay_name'] = $goods_bill ['pay_name'] ;
		$result['cart_st'][0]['son_c'][0]['m_html'] = $goods_bill ['m_html'] ;
		$result['cart_st'][0]['son_c'][0]['pay_id'] = $goods_bill["pay_id"] ;		
		$result['cart_st'][0]['son_c'][0]['shop_area_ids'] = $goods_bill['shop_area_ids'] ;
		$result['cart_st'][0]['son_c'][0]['colorkey'] = $goods_bill['colorkey'] ;
		$result['cart_st'][0]['son_c'][0]['speckey'] = $goods_bill['speckey'] ;
		$result['cart_st'][0]['son_c'][0]['color_n'] = $goods_bill['color_n'] ;
		$result['cart_st'][0]['son_c'][0]['spec_n'] = $goods_bill['spec_n'] ;
		$result['cart_st'][0]['son_c'][0]['color'] = $goods_bill['color'] ;
		$result['cart_st'][0]['son_c'][0]['spec'] = $goods_bill['spec'] ;
		$result['cart_st'][0]['son_c'][0]['goods_freight'] = $goods_bill['goods_freight'] ;
		$result['pay_id'] = $goods_bill ['pay_id'] ;	
								
		//商品运费
		$result ['goods_freight'][$goods_bill['st_id']] = floatval($goods_bill['goods_freight']);
		//商品总金额
		$result ['total'][$goods_bill['st_id']] = (intval($goods_nums) * floatval($goods_bill['price']));
		//商品总积分
		$result ['amount_points'][$goods_bill['st_id']] = (intval($goods_nums) * floatval($goods_bill['point_num'])) ;
		//总增值币
		$result ['amount_coin'][$goods_bill['st_id']] =(intval($goods_nums) * floatval($goods_bill['coin_num'])) ;
		
		//商品金额	
		$result['cart_st'][0]['son_c'][0]['amount'] = $result ['total'][$goods_bill['st_id']] ;
		//商品积分
		$result['cart_st'][0]['son_c'][0]['amount_points'] = $result ['amount_points'][$goods_bill['st_id']] ;
		//商品增值币
		$result['cart_st'][0]['son_c'][0]['amount_coin'] = $result ['amount_coin'][$goods_bill['st_id']] ;	
		
		//总金额
		$result ['alltotal'] = $result ['total'][$goods_bill['st_id']] + $goods_bill ['goods_freight'] ;
		//总积分	
		$result ['allpoints'] = $result ['amount_points'][$goods_bill['st_id']] ;
		//总增值币
		$result ['allcoins'] = $result ['amount_coin'][$goods_bill['st_id']] ;			
		
						
		return $result ;		
	}
	


	
}